<?php
session_start();
if (!isset($_SESSION["userid"])) {
    header("Location: /login.html");
    usleep(2 * 1000000);
    exit;
} ?>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息管理系统</title>
    <link rel="stylesheet" href="css/model.css">
    <style type="text/css">
        body {
            font-family: Arial, sans-serif;
            background-color: #f3f4f6;
            margin: 0;
            padding: 20px;
        }

        .box {
            max-width: 800px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h2 {
            background-color: aliceblue;
            padding: 10px;
            border-radius: 5px;
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th {
            text-align: right;
            color: #333;
        }

        td {
            border: none;
        }

        input[type="text"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        .btn {
            background-color: lightskyblue;
            border: none;
            border-radius: 5px;
            padding: 8px 12px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-right: 10px;
        }

        .btn:hover {
            background-color: deepskyblue;
        }

        .btn-submit {
            background-color: #007bff;
            color: white;
        }

        .btn-submit:hover {
            background-color: #0056b3;
        }

        .radio-group {
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .radio-group label {
            margin-right: 15px;
            font-size: 14px;
        }
    </style>
</head>
<body>
<div class="box">
    <h2>修改学生信息</h2>
    <div>
        <form id="edit-info">
            <table border="1">
                <tr>
                    <th>学生编号：</th>
                    <td><input type="text" class="id" name="id" readonly></td>
                </tr>
                <tr>
                    <th>姓　　名：</th>
                    <td><input type="text" class="name" name="name" size="25" required></td>
                </tr>
                <tr>
                    <th>性　　别：</th>
                    <td class="radio-group">
                        <label><input class="sex-me" type="radio" name="sex" value="男">男</label>
                        <label><input class="sex-fe" type="radio" name="sex" value="女">女</label>
                    </td>
                </tr>
                <tr>
                    <th>年　　龄：</th>
                    <td><input type="text" class="age" name="age" size="25"></td>
                </tr>
                <tr>
                    <th>籍　　贯：</th>
                    <td><input type="text" class="city" name="city" size="25"></td>
                </tr>
                <tr>
                    <th></th>
                    <td>
                        <input type="button" class="btn" onClick="javascript :history.back(-1);" value="返回">
                        <input type="submit" class="btn btn-submit" value="更新">
                    </td>
                </tr>
            </table>
        </form>
    </div>

    <div id="messageModel" class="modal">
        <div class="modal-content">
            <span class="close" id="closeModal">&times;</span>
            <p id="modalMessage"></p>
        </div>
    </div>
</div>
<script src="js/common.js"></script>
<script>
    window.onload = () => {
        // 获取当前页面的URL
        const url = window.location.href;
        const searchParams = new URLSearchParams(new URL(url).search);
        const infoForm = document.querySelector('#edit-info');
        const id = searchParams.get('id');
        if (id) {
            queryById(id);

            infoForm.addEventListener('submit', evt => {
                evt.preventDefault();

                const formData = new FormData(infoForm);

                const updateObj = {};

                formData.forEach((value, key) => {
                    updateObj[key] = value;
                });

                updateInfo(updateObj);
                return false;
            })
        }

    };

    const updateInfo = info => {
        fetch(`dispatch.php?controller=StudentManage&service=updateInfo`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(info),
        }).then(res => {
            if (res.status === 200) {
                showModal('更新成功！')
            }
        })
    }


    const queryById = id => {
        fetch(`dispatch.php?controller=StudentManage&service=getById&id=${id}`, {
            method: 'GET'
        }).then(response => response.json())
            .then(data => {
                const oldId = document.querySelector(".id");
                const name = document.querySelector(".name");
                const age = document.querySelector(".age")
                const city = document.querySelector(".city");
                const sexMe = document.querySelector(".sex-me");
                const sexFe = document.querySelector(".sex-fe");

                oldId.value = data.id;
                name.value = data.name;
                age.value = data.age;
                city.value = data.city;
                let sex = data.sex;
                if ('男' === sex) {
                    sexMe.setAttribute('checked', 'true');
                } else {
                    sexFe.setAttribute('checked', 'true');
                }
            })
    }
</script>
</body>
</html>
